package com.work.keep.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.work.keep.R;
import com.work.keep.activity.SingleWorkoutActivity;
import com.work.keep.datas.RecommendData;
import com.work.keep.volley.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/21.
 */
public class RecommendAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<RecommendData.DataEntity.PlansEntity> plans;
    private List<RecommendData.DataEntity.WorkoutsEntity> workouts;
    VolleySingleton volleySingleton;
    private ImageLoader imageLoader;

    public RecommendAdapter(Context context) {
        this.context = context;
        plans = new ArrayList<>();
        workouts = new ArrayList<>();
        volleySingleton = VolleySingleton.getInstance();
        imageLoader = volleySingleton._getImageLoader();
    }

    public void addData(List<RecommendData.DataEntity.PlansEntity> plans, List<RecommendData.DataEntity.WorkoutsEntity> workouts) {
        this.plans = plans;
        notifyDataSetChanged();
        this.workouts = workouts;
        notifyDataSetChanged();

        Log.d("aaa", plans.size() + "");
    }



    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v;
        if (viewType == 0) {
            v = LayoutInflater.from(context).inflate(R.layout.activity_playground_index_item, parent, false);
            return new IndexViewHolder(v);
        }
        else if (viewType == 4) {
            v = LayoutInflater.from(context).inflate(R.layout.activity_playground_index_item, parent, false);
            return new IndexViewHolder(v);
        }else {
        v = LayoutInflater.from(context).inflate(R.layout.activity_playground_workout_item, parent, false);
        return new PlansViewHolder(v);
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (position == 0) {
            ((IndexViewHolder) holder).playgroundIndex.setText("训练计划");
        }
        if (position == 4) {
            ((IndexViewHolder) holder).playgroundIndex.setText("单次训练");
        }
        if (plans.size()>0&&position > 0 && position < 4) {
            PlansViewHolder plansViewHolder = ((PlansViewHolder) holder);
            plansViewHolder.plansPicture.setVisibility(View.VISIBLE);
            plansViewHolder.plansPicture.setDefaultImageResId(R.mipmap.plan320_160);
            plansViewHolder.plansPicture.setErrorImageResId(R.mipmap.plan320_160);
            plansViewHolder.plansPicture.setImageUrl(plans.get(position - 1).getPicture(), imageLoader);
            plansViewHolder.planName.setText(plans.get(position - 1).getName());
            plansViewHolder.planName.setTextColor(Color.WHITE);
            plansViewHolder.planLevel.setText("K" + plans.get(position - 1).getDifficulty());
            plansViewHolder.planLevel.setTextColor(Color.WHITE);
            plansViewHolder.planEquipment.setText(plans.get(position - 1).getEquipments().get(0).getName());
            plansViewHolder.planEquipment.setTextColor(Color.WHITE);
            plansViewHolder.daysAndTranspoint.setText(plans.get(position - 1).getDays() + plans.get(position - 1).getTrainingpoints().get(0).getName());
            plansViewHolder.daysAndTranspoint.setTextColor(Color.WHITE);
            plansViewHolder.pioneer.setText(plans.get(position - 1).getPioneer() + "");
            plansViewHolder.pioneer.setTextColor(Color.WHITE);
        }
        if (workouts.size()>0&&position > 4) {
            PlansViewHolder plansViewHolder = ((PlansViewHolder) holder);
            plansViewHolder.plansPicture.setVisibility(View.GONE);
            plansViewHolder.planName.setText(workouts.get(position - 5).getName());
            plansViewHolder.planLevel.setText("K" + workouts.get(position - 5).getDifficulty());
            plansViewHolder.planEquipment.setText(workouts.get(position - 5).getEquipments().get(0).getName());
            plansViewHolder.daysAndTranspoint.setText(workouts.get(position - 5).getDuration() + workouts.get(position - 5).getTrainingpoints().get(0).getName());
            plansViewHolder.pioneer.setText(workouts.get(position - 5).getPioneer() + "");
            plansViewHolder.planName.setTextColor(Color.BLACK);
            plansViewHolder.planLevel.setTextColor(Color.BLACK);
            plansViewHolder.planEquipment.setTextColor(Color.BLACK);
            plansViewHolder.daysAndTranspoint.setTextColor(Color.BLACK);
            plansViewHolder.pioneer.setTextColor(Color.BLACK);
            plansViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SingleWorkoutActivity.goToSingleWorkoutActivity(context,workouts.get(position-5).get_id());
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return 10;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    class PlansViewHolder extends RecyclerView.ViewHolder {
        NetworkImageView plansPicture;
        TextView planName, planLevel, daysAndTranspoint, pioneer, planEquipment;

        public PlansViewHolder(View itemView) {
            super(itemView);
            plansPicture = (NetworkImageView) itemView.findViewById(R.id.plansPicture);
            planName = (TextView) itemView.findViewById(R.id.planName);
            planLevel = (TextView) itemView.findViewById(R.id.planLevel);
            daysAndTranspoint = (TextView) itemView.findViewById(R.id.daysAndTranspoint);
            pioneer = (TextView) itemView.findViewById(R.id.pioneer);
            planEquipment = (TextView) itemView.findViewById(R.id.planEquipment);
        }
    }

    class IndexViewHolder extends RecyclerView.ViewHolder {
        TextView playgroundIndex;

        public IndexViewHolder(View itemView) {
            super(itemView);
            playgroundIndex = (TextView) itemView.findViewById(R.id.playgroundIndex);
        }
    }
}
